package com.qxy.hgz.util;

import com.qxy.hgz.rank.model.Movie;
import com.qxy.hgz.rank.model.TVSeries;
import com.qxy.hgz.rank.model.Variety;
import com.qxy.hgz.rank.model.Video;
import com.qxy.hgz.rank.model.VideoType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 将Json转换为Java类
 */
public class JsonUtil {

    /**
     * 解析token的方法
     * @param response 请求token时，返回的完整报文
     * @return 返回一个token字符串类型
     */
    public static String parseToken(JSONObject response) {
        try {
            JSONObject data = response.getJSONObject("data");
            return data.getString("access_token");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("解析Token失败");
    }

    /**
     *
     * @param json 通过这个json对象构建出通用的Video对象
     * @return 返回一个Video对象
     * @throws JSONException
     */
    public static Video getVdieo(JSONObject json) throws JSONException {
        List<String> actorList = null;
        List<String> dirctorList = null;
        JSONArray actorArray = json.optJSONArray("actors");
        actorList = getListFromJsonArray(actorArray);
        JSONArray dirctorArray = json.optJSONArray("directors");
        dirctorList = getListFromJsonArray(dirctorArray);
        int hot = json.optInt("hot");
        String name = json.optString("name");
        String imageUrl = json.optString("poster");
        String date = json.optString("release_date");
        int type = json.optInt("type");
        return new Video(actorList, dirctorList, hot, name, imageUrl, date, VideoType.parseType(type));
    }


    /**
     * 通过反射动态生成派生类
     * @param json 对应的Json对象
     * @param tClass 要动态生成类的类型信息
     * @param <T> 具体的类
     * @return 返回需要的类
     * @throws NoSuchMethodException
     * @throws JSONException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static<T> T getEntity(JSONObject json,Class<T> tClass) throws NoSuchMethodException, JSONException,
            InvocationTargetException, IllegalAccessException, InstantiationException {
        Constructor<T> constructor;
        T entity;
        if (tClass.equals(Movie.class)) {
            constructor = tClass.getDeclaredConstructor(Video.class);
            entity = constructor.newInstance(getVdieo(json));
        }else if (tClass.equals(Variety.class)) {
            constructor = tClass.getDeclaredConstructor(Video.class,String.class);
            Video video = getVdieo(json);
            String enName = json.optString("name_en");
            entity = constructor.newInstance(video,enName);
        }else if (tClass.equals(TVSeries.class)) {
            constructor = tClass.getDeclaredConstructor(Video.class,String.class,List.class);
            Video video = getVdieo(json);
            String enName = json.optString("name_en");
            List<String> tags = getListFromJsonArray(json.optJSONArray("tags"));
            entity = constructor.newInstance(video,enName,tags);
        }else {
            throw new RuntimeException("无此类型信息");
        }
        return entity;
    }

    /**
     * 将json数组转换成List类型的对象
     * @param array 要解析的Json数组
     * @return 返回一个List类型的对象
     * @throws JSONException
     */
    public static List<String> getListFromJsonArray(JSONArray array) throws JSONException {
        List<String> list = new ArrayList<>();
        if (array == null) return null;
        for (int i = 0; i < array.length(); i++) {
            list.add(array.getString(i));
        }
        return list;
    }


    /**
     * 生成对应类的集合
     * @param array 对应的Json数组
     * @param tClass 对应类的类型信息
     * @param <T> 声明的类
     * @return
     * @throws JSONException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static<T> List<T> getList(JSONArray array, Class<T> tClass) throws JSONException, InvocationTargetException,
            NoSuchMethodException, IllegalAccessException, InstantiationException {
        List<T> list = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            T entity = JsonUtil.getEntity(array.getJSONObject(i),tClass);
            list.add(entity);
        }
        return list;
    }

    /**
     * 请求榜单数据时，将数据集合解析
     * @param json 请求榜单时的完整报文
     * @return 返回数据集合的json数组
     * @throws JSONException
     */
    public static JSONArray parseListData(JSONObject json) throws JSONException {
        return json.getJSONObject("data").getJSONArray("list");
    }

    public static boolean isSuccessed(JSONObject json) {
        int errorCode = -1;
        try {
            errorCode = json.getJSONObject("data").getInt("error_code");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return errorCode == 0;
    }

    public static String getOpenId(){
        return "";
    }

}
